package WebRoot.demo8;

import java.util.UUID;

import org.hoyi.DB.ctrl.FILTER;
import org.hoyi.dishop.Hoyipage;
import org.hoyi.wb.comment.RequestMode;
import org.hoyi.wb.comment.RequestType;

@RequestMode(MODE = { RequestType.GET, RequestType.POST })
public class DemoCtr extends Hoyipage {

	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void insertUser() {
		String userId = UUID.randomUUID().toString();
		String username = this.getParams("username");
		String usertype = this.getParams("usertype");
		String createtime = this.getParams("createtime");
		String notes = this.getParams("notes");

		user u = new user();
		u.setUserid(userId);
		u.setUsername(username);
		u.setUsertype(usertype);
		u.setCreatetime(createtime);
		u.setNotes(notes);
		int result = u.Insert();
		if (result == 1) {
			this.WriteUTF8JSONDATAMSG(1, "success", "");
		} else {
			this.WriteUTF8JSONDATAMSG(1, "fail", "");
		}
	}

	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void updateUser() {
		String userId = this.getParams("userId");
		String username = this.getParams("username");

		user u = user.E().Where(user.userid.Equals(userId)).First();
		u.setUsername(username);
		int result = u.Update();
		if (result == 1) {
			this.WriteUTF8JSONDATAMSG(1, "success", u);
		} else {
			this.WriteUTF8JSONDATAMSG(1, "fail", "");
		}
	}

	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void filterTest() {
		String username = this.getParams("username");
		String usertype = this.getParams("usertype");
		String createtime = this.getParams("createtime");
		String notes = this.getParams("notes");
		FILTER filter = user.username.Equals(username);
		filter = filter.AND(user.usertype.Equals(usertype));

		user u = user.E().Where(filter).First();
		if (u != null) {
			this.WriteUTF8JSONDATAMSG(1, "success", u);
		} else {
			this.WriteUTF8HTML("error");
		}
	}

}
